<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\portal\controller;

use app\admin\model\NavMenuModel;
use cmf\controller\AdminBaseController;
use plugins\helper\Date;
use think\facade\Db;

class AdminMerchController extends AdminBaseController
{
    public function index()
    {
        $list = Db::name('merch')->alias('m')
            ->leftJoin('nav_menu nm', 'm.category = nm.id')
            ->field('m.* , nm.name as category_name')
            ->paginate(20);

        $data = $list->all();

        foreach ($data as $key => &$item) {
             switch ($item['status']) {
                case 1 :
                    $item['status_name'] = '通过';
                    break;
                case -1 :
                    $item['status_name'] = '拒绝';
                    break;
                case 0 :
                    $item['status_name'] = '待审核';
                    break;
            }
            $item['category_name'] = NavMenuModel::where(['id' => $item['category']])->field('name')->find()['name'];
            $list[$key] = $item;
        }
        unset($data);
        $this->assign('page', $list->render());//单独提取分页出来
        $this->assign('demo', $list);
        return $this->fetch();
    }

    /**
     * 通过
     */
    public function pass()
    {
        $id = $this->request->get('id');
        Db::name('merch')->where('id',$id)->update(['status'=>1,'reason'=>'','deal_time'=>Date::now()]);
        $this->success('更新成功');
    }

    /**
     * 拒绝
     */
    public function refuse()
    {
        $id = $this->request->get('id');
        Db::name('merch')->where('id',$id)->update(['status'=>-1,'reason'=>'管理员拒绝','deal_time'=>Date::now()]);
        $this->success('更新成功');
    }

    /**
     * 拒绝
     */
    public function delete()
    {
        $id = $this->request->get('id');
        Db::name('merch')->where('id',$id)->delete();
        $this->success('更新成功');
    }

}
